define(['text!./detailSelf.html', 'css!./detailSelf.css','APPdate'], function( html) {
	avalon.templateCache['detailSelf']=html;
	var vm = avalon.define({
	    $id: "detailSelf",
	    role:'self',
	    realName: '',
		age: '',
		date: '',
	    choiceDate:function(id){
	    	date(id);
	    },
	    relation:function(){
	    	var family = JSON.parse(getstorage('family'));
			var height = $("#height").find("option:selected").val();
			var weight = $("#weight").find("option:selected").val();
			var date = $('#date').val();
			if(!vm.realName){
				msgbox('提示','请填写您的真实姓名');
				return;
			}
			if(!vm.sex){
				msgbox('提示','请选择您的性别');
				return;
			}
			if(!date){
				msgbox('提示','请输入您的出生年月');
				return;
			}
			family = {
				name: family.name,
				phone: family.phone,
				tel: family.tel,
				realName: vm.realName,
				sex: vm.sex,
				age: vm.age.replace('周岁',''),
				date: date,
				height: height,
				weight: weight
			}
			setstorage('family', family);
	    	Router.go('/relation/relation',JSON.stringify(vm.role));
	    },
	    back:function(){
	    	Router.go('/selfInfo/selfInfo');
	    },
	    sex: 1,
		changeSex: function() {
			var index = $(this).index();
			if(index == 0) {
				vm.sex = 1;
			} else {
				vm.sex = 0;
			}
			$(this).addClass('current').siblings().removeClass('current');
		},
		height: [],
		weight: [],
		init: function() {
			vm.weight = [];
			vm.height = [];
			for(var i = 0; i < 200; i++) {
				vm.weight.push(i + 1);
			}
			for(var i = 0; i < 250; i++) {
				vm.height.push(i + 1);
			}
			document.getElementById("height").options[159].selected = "selected";
			document.getElementById("weight").options[54].selected = "selected";
			var family=JSON.parse(getstorage('family'))
			if(family.realName){
				vm.realName=family.realName
			}else{
				vm.realName="";
			}
			if(family.date){
				vm.date=family.date;
			}else{
				vm.date="";
			}
		}
  });
});

//选择家人的出生年月
function date(el) {
	appDateField = new APPDateField(el, {
		title: '请选择日期',
		isCN: true,
		commer: '-',
		upYear: 100,
		downYear: 0,
		showTime: false,
		complete: function() {
			var birth = this.getCurrentDate()
			console.log(birth);
			var birthYear = birth[0];
			var birthMonth = birth[1];
			var birthDay = birth[2];
			var d = new Date();
			var nowYear = d.getFullYear();
			var nowMonth = d.getMonth() + 1;
			var nowDay = d.getDate();
			if(nowYear == birthYear) {
				returnAge = 0; //同年 则为0岁
			} else {
				var ageDiff = nowYear - birthYear; //年之差
				if(ageDiff > 0) {
					if(nowMonth == birthMonth) {
						var dayDiff = nowDay - birthDay; //日之差
						if(dayDiff < 0) {
							returnAge = ageDiff - 1;
						} else {
							returnAge = ageDiff;
						}
					} else {
						var monthDiff = nowMonth - birthMonth; //月之差
						if(monthDiff < 0) {
							returnAge = ageDiff - 1;
						} else {
							returnAge = ageDiff;
						}
					}
				} else {
					returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
				}
			}
			console.log(returnAge);
			$('#age').val(returnAge+'周岁');
		},
		cancel: function() {
			console.log('[已取消]  Cancel----');
		}
	});
}

function changeDate(num) {
	var age=num.replace('周岁','');
	var birthday = new Date();
	var year = birthday.getFullYear();
	var moth = birthday.getMonth() + 1;
	var day = birthday.getDate();
	if(age == undefined) {
		var result = year + "-" + moth + "-" + day;
		$('#date').val(result);
	} else {
		var result = ((year - age) + "-" + moth + "-" + day);
		$('#date').val(result);
	}
}

function changeBMI(){
	var height = $("#height").find("option:selected").val();
	var weight = $("#weight").find("option:selected").val();
	console.log(height,weight);
	var BMI=(weight/((height*0.01)*(height*0.01))).toFixed(2);
	if(BMI<18.5){
		$('#BMI').html(BMI);
		$('#notice').html('偏轻');
	}else if(BMI>=18.5&&BMI<25){
		$('#BMI').html(BMI);
		$('#notice').html('正常');
	}else{
		$('#BMI').html(BMI);
		$('#notice').html('偏重');
	}
	
}